home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / motif / simple-popup.c.z / simple-popup.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  4.4 KB  |  168 lines

  1. /*
  2.  * simple-popup - simple single buffered RGBA motif program with a popup menu.
  3.  */
  4. /* compile: cc -o simple-popup simple-popup.c -lGLw -lGL -lXm -lXt -lX11 */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <Xm/Frame.h>
  8. #include <Xm/RowColumn.h>
  9. #include <X11/GLw/GLwMDrawA.h>
  10. #include <X11/keysym.h>
  11. #include <X11/Xutil.h>
  12. #include <GL/glx.h>
  13.  
  14. static int attribs[] = { GLX_RGBA, GLX_RED_SIZE, 1, None};
  15.  
  16. static String fallbackResources[] = {
  17.     "*sgiMode:    True",
  18.     "*useSchemes: all",
  19.     "*glxwidget*width: 300", "*glxwidget*height: 300",
  20.     "*frame*shadowType: SHADOW_IN",
  21.     NULL};
  22.  
  23. void
  24. draw_scene(void) {
  25.    glClearColor(0.5, 0.5, 0.5, 1.0);
  26.    glClear(GL_COLOR_BUFFER_BIT);
  27.    glColor3f(1.0,0.0,0.0);
  28.    glRectf(-.5,-.5,.5,.5);
  29.    glColor3f(0.0,1.0,0.0);
  30.    glRectf(-.4,-.4,.4,.4);
  31.    glColor3f(0.0,0.0,1.0);
  32.    glRectf(-.3,-.3,.3,.3);
  33.    glFlush();
  34. }
  35.  
  36. static void
  37. input(Widget w, XtPointer client_data, XtPointer call) {
  38.    char buf[31];
  39.    KeySym keysym;
  40.    XComposeStatus composeStatus;
  41.    XEvent *event = ((GLwDrawingAreaCallbackStruct *) call)->event;
  42.  
  43.    switch(event->type) {
  44.    case KeyRelease:
  45.       XLookupString(&event->xkey, buf, sizeof buf, &keysym, &composeStatus);
  46.       switch(keysym) {
  47.       case XK_Escape :
  48.      exit(EXIT_SUCCESS);
  49.      break;
  50.       default: break;
  51.       }
  52.       break;
  53.    }
  54. }
  55.  
  56. static void
  57. resize(Widget w, XtPointer client_data, XtPointer call) {
  58.    GLwDrawingAreaCallbackStruct *call_data;
  59.    call_data = (GLwDrawingAreaCallbackStruct *) call;
  60.  
  61.    glViewport(0, 0, call_data->width, call_data->height);
  62. }
  63.  
  64. static void
  65. expose(Widget w, XtPointer client_data, XtPointer call) {
  66.     GLwDrawingAreaCallbackStruct *call_data;
  67.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  68.  
  69.     draw_scene();
  70. }
  71.  
  72. static void
  73. menu(Widget w, XtPointer clientData, XtPointer callData) {
  74.     int button = (int) clientData;
  75.     Bool redraw = False;
  76.  
  77.     switch (button) {
  78.     case 0:
  79.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  80.     redraw = True;
  81.     break;
  82.     case 1:
  83.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  84.     redraw = True;
  85.     break;
  86.     case 2:
  87.     exit(EXIT_SUCCESS);
  88.     break;
  89.     default:
  90.     break;
  91.     }
  92.     
  93.     if (redraw)
  94.     draw_scene();
  95. }
  96.  
  97. static void
  98. activate_menu(Widget w, XtPointer clientData, XEvent *event, Boolean *cont) {
  99.     Widget popup = *((Widget *) clientData);
  100.  
  101.     if (event->type == ButtonPress && event->xbutton.button == Button3) {
  102.     XmMenuPosition(popup, &event->xbutton);
  103.     XtManageChild(popup);
  104.     }
  105. }
  106.  
  107. static void
  108. create_popup(Widget parent) {
  109.     Arg args[10];
  110.     static Widget popup;
  111.     int n;
  112.     XmButtonType button_types[] = {
  113.     XmPUSHBUTTON, XmPUSHBUTTON, XmSEPARATOR, XmPUSHBUTTON,
  114.     };
  115.     XmString button_labels[XtNumber(button_types)];
  116.  
  117.     button_labels[0] = XmStringCreateLocalized("draw filled");
  118.     button_labels[1] = XmStringCreateLocalized("draw lines");
  119.     button_labels[2] = NULL;
  120.     button_labels[3] = XmStringCreateLocalized("quit");
  121.  
  122.     n = 0;
  123.     XtSetArg(args[n], XmNbuttonCount, XtNumber(button_types)); n++;
  124.     XtSetArg(args[n], XmNbuttonType, button_types); n++;
  125.     XtSetArg(args[n], XmNbuttons, button_labels); n++;
  126.     XtSetArg(args[n], XmNsimpleCallback, menu); n++;
  127.     popup = XmCreateSimplePopupMenu(parent, "popup", args, n);
  128.     XtAddEventHandler(parent, ButtonPressMask, False, activate_menu, &popup);
  129.  
  130.     XmStringFree(button_labels[0]);
  131.     XmStringFree(button_labels[1]);
  132.     XmStringFree(button_labels[3]);
  133. }
  134.  
  135. main(int argc, char *argv[]) {
  136.     Display        *dpy;
  137.     XtAppContext    app;
  138.     XVisualInfo    *visinfo;
  139.     GLXContext      glxcontext;
  140.     Widget          toplevel, frame, glxwidget;
  141.  
  142.     toplevel = XtOpenApplication(&app, "simple-popup", NULL, 0, &argc, argv,
  143.         fallbackResources, applicationShellWidgetClass, NULL, 0);
  144.     dpy = XtDisplay(toplevel);
  145.  
  146.     frame = XmCreateFrame(toplevel, "frame", NULL, 0);
  147.     XtManageChild(frame);
  148.  
  149.     /* specify visual directly */
  150.     if (!(visinfo = glXChooseVisual(dpy, DefaultScreen(dpy), attribs)))
  151.     XtAppError(app, "no suitable RGB visual");
  152.  
  153.     glxwidget = XtVaCreateManagedWidget("glxwidget", glwMDrawingAreaWidgetClass,
  154.     frame, GLwNvisualInfo, visinfo, NULL);
  155.     XtAddCallback(glxwidget, GLwNexposeCallback, expose, NULL);
  156.     XtAddCallback(glxwidget, GLwNresizeCallback, resize, NULL);
  157.     XtAddCallback(glxwidget, GLwNinputCallback, input, NULL);
  158.  
  159.     create_popup(frame);
  160.  
  161.     XtRealizeWidget(toplevel);
  162.  
  163.     glxcontext = glXCreateContext(dpy, visinfo, 0, GL_TRUE);
  164.     GLwDrawingAreaMakeCurrent(glxwidget, glxcontext);
  165.  
  166.     XtAppMainLoop(app);
  167. }
  168.